home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / swtools / generic / gizmos / socketstuff.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  144 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <sys/signal.h>
  24. #include <sys/fcntl.h>
  25. #include <sys/errno.h>
  26. #include <string.h>
  27. #include <netinet/in.h>
  28. #include <sys/stat.h>
  29. #include <netdb.h>
  30.  
  31. extern long  sock;
  32.  
  33. void createsocket(long portnum)
  34. {
  35.     struct    sockaddr_in saddr;
  36.  
  37.     /* Create a socket. */
  38.     sock = socket(AF_INET, SOCK_STREAM, 0);
  39.     if(sock < 0) {
  40.         perror("opening stream socket");
  41.         exit(1);
  42.     }
  43.     if(sock == 0) {
  44.             sock = dup((int)sock);
  45.     }
  46.     /* Construct name for parent socket on local machine using portnum. */
  47.  
  48.     saddr.sin_family = AF_INET;
  49.     saddr.sin_addr.s_addr = INADDR_ANY;
  50.     saddr.sin_port = (unsigned short)htons(portnum);
  51.     if(connect((int)sock, &saddr, sizeof(saddr)) < 0) {
  52.         perror("connecting socket");
  53.         exit(1);
  54.     }
  55. }
  56.  
  57. void sendpair(long dev, long val)
  58. {
  59.     long    buf[2];
  60.  
  61.     if (sock == -1) {
  62.     printf("sendpair (%d,%d)\n",dev, val);
  63.     return;
  64.     }
  65.     buf[0] = dev;
  66.     buf[1] = val;
  67.  
  68.     if(write((int)sock, buf, sizeof(buf)) < 0) {
  69.     perror("writing stream message");
  70.     exit(1);
  71.     }
  72. }
  73.  
  74. void sendcmd(long dev)
  75. {
  76.     sendpair(dev, 0);
  77. }
  78.  
  79. void sendsocket(char *buf, long size)
  80. {
  81.  
  82.     if (sock == -1) {
  83.     return;
  84.     }
  85.  
  86.     if(write((int)sock, buf, (unsigned int)size) < 0) {
  87.     perror("writing stream message");
  88.     exit(1);
  89.     }
  90. }
  91.  
  92. long readbuf(int fd,char *p, int totbytes)
  93. {
  94.     int nbytes, n;
  95.  
  96.     if (sock == -1) return 0;
  97.     nbytes = 0;
  98.     while (nbytes < totbytes) {
  99.     if ( (n=read(fd,p+nbytes,totbytes-nbytes)) <= 0) {
  100.         perror("gizmo read failure");
  101.         exit(0);
  102.     }
  103.     nbytes += n;
  104.     }
  105.     return 0;
  106. }
  107.  
  108. void readsocket(char *c, long size)
  109. {
  110.     if (readbuf((int)sock,c,(int)size) < 0)
  111.     exit(1);
  112. }
  113.  
  114. long readlongfromsocket()
  115. {
  116.     long val;
  117.  
  118.     if (readbuf((int)sock,(char *)&val,(int)sizeof(val)) < 0) {
  119.     fprintf(stderr,"unknown data from main program\n");
  120.     exit(0);
  121.     }
  122.     return val;
  123. }
  124.  
  125. void sendascii(long type, char *str)
  126. {
  127.     long    buf[210], i = 2;
  128.  
  129.     if (sock == -1) return;
  130.     buf[0] = type;
  131.     buf[1] = -1;
  132.     if (str != 0)
  133.     while (*str) {
  134.         buf[i++] = type;
  135.         buf[i++] = *str++;
  136.     }
  137.     buf[i++] = type;
  138.     buf[i++] = 0;
  139.     if(write((int)sock, buf, (unsigned int)i*sizeof(long)) < 0) {
  140.     perror("writing stream message");
  141.     exit(1);
  142.     }
  143. }
  144.